let url = 'wss://pms.youyacao.com/ws';
// let url = 'ws://172.16.16.9:5200/ws';
let reconnectInterval = null;
let maxReconnectAttempts = 60; // 每分钟尝试20次，总共3分钟
let reconnectDelay = 3000;
let reconnectAttempts = 0; // 重连尝试次数
let socket = null;
let customEventData = {
    type: 'value1',
    data: 'value2',
    // ...
};
window.addEventListener('resize', function () {
    console.log('当前窗口宽度:', window.innerWidth);
});
console.log(window.innerWidth);

function createWebSocket() {
    socket = new WebSocket(url);

    socket.addEventListener('open', function (event) {
        console.log('连接成功');
        reconnectAttempts = 0; // 成功连接后重置尝试次数
        // customEventData.type = 'getStatistics';
        // socket.send(JSON.stringify(customEventData));
        setInterval(function () {
            customEventData.type = 'getStatistics';
            socket.send(JSON.stringify(customEventData));
            customEventData.type = 'webconfig';
            socket.send(JSON.stringify(customEventData));
            customEventData.type = 'Region';
            socket.send(JSON.stringify(customEventData));
            customEventData.type = 'getProjectTop';
            socket.send(JSON.stringify(customEventData));
            customEventData.type = 'getProjectProgress';
            socket.send(JSON.stringify(customEventData));
            customEventData.type = 'projectManth';
            socket.send(JSON.stringify(customEventData));
            customEventData.type = 'getStatistics';
            socket.send(JSON.stringify(customEventData));
        }, 1000 * num);
        clearTimeout(reconnectInterval); // 清除可能存在的重连定时器
    });

    socket.addEventListener('message', function (res) {
        var data = JSON.parse(res.data);
        // ... 处理消息逻辑保持不变
        if (data.type === 'welcome') {
            console.log('欢迎消息:', data.message);
        } else if (data.type === 'getProjectProgress') {
            xiaosouList(data.data);
        } else if (data.type === 'getStatistics') {
            Statistics(data.data);
            numGoFn(data.data);
        } else if (data.type === 'getProjectTop') {
            xiaosouTop(data.data);
        } else if (data.type === 'Region') {
            Region(data.data);
            pieDataFn(0, data.data);
            var monthIndex = 0;
            var bNum = 0;
            var startN = 0;
            var timer = null;
            setVal(data.data, timer, startN, bNum, monthIndex);
        } else if (data.type === 'projectManth') {
            manth(data.data);
        } else if (data.type === 'webconfig') {
            $('.month-tip').text(data.data.datetime);
        } else {
            console.log('未知消息类型:', data);
        }
    });

    socket.addEventListener('error', function (error) {
        console.error('连接错误', socket.readyState, error);
    });

    socket.addEventListener('close', function (event) {
        console.log('连接关闭', event.code, event.reason);
        handleDisconnect();
    });
}

function handleDisconnect() {
    if (reconnectAttempts < maxReconnectAttempts) {
        console.log(`Reconnecting attempt ${reconnectAttempts + 1}...`);
        reconnectAttempts++;
        reconnectInterval = setTimeout(createWebSocket, reconnectDelay);
    } else {
        console.log('Failed to reconnect after maximum attempts. Stopping reconnection.');
        reconnectInterval = null;
    }
}

createWebSocket(); // 首次尝试连接
var list = $('#scrollList');
var scrollHeight = list[0].scrollHeight;
var scrollTop = list.scrollTop();
var scrollInterval = 5000; // 每隔5秒滚动一次
var direction = 1; // 1 代表向下滚动，-1 代表向上滚动
console.log(list, scrollHeight, scrollTop);
function autoScroll() {
    if (scrollTop === 0 && direction === 1) {
        direction = -1;
        list.animate({scrollTop: scrollHeight}, 500);
    } else if (scrollTop === scrollHeight && direction === -1) {
        direction = 1;
        list.animate({scrollTop: 0}, 500);
    } else if (direction === 1) {
        list.animate({scrollTop: scrollTop + 1}, 500);
    } else {
        list.animate({scrollTop: scrollTop - 1}, 500);
    }
}

setInterval(autoScroll, scrollInterval);
console.log(list, scrollHeight, scrollTop);

function refreshPageAfter(seconds) {
    setTimeout(function () {
        location.reload();
    }, seconds * 1000);
}

refreshPageAfter(300); // 页面将在300秒后刷新
